package com.cruel_wandering.wifi_geo_predictor.wifi_simulation;

import java.util.LinkedList;
import java.util.List;

import com.cruel_wandering.wifi_geo_predictor.Logger;

import android.content.Context;
import android.location.Location;
import android.net.DhcpInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;

public class WifiManagerSimpleSimulator extends MyWifiManager
{
  private WifiSimDB wifiSimDB;
  private boolean enabled;
  private LinkedList<MyScanResult> scanResults;
  private int counter = -1;
  private Context ctx;

  /** Initiate new Wifi simulator using given parameters.
   * @param ctx the context
   * @param dbTableName table name in the database
   * @param enabled is the manager enabled by default
   */
  public WifiManagerSimpleSimulator(Context ctx, String dbTableName,
    boolean enabled)
  {
	this.ctx = ctx;
    this.wifiSimDB = new WifiSimDB(ctx, dbTableName);
    this.enabled = enabled;
  }

	@Override
  public int addNetwork(WifiConfiguration config)
  {
	  // TODO Auto-generated method stub
	  return 0;
  }

	@Override
  public WifiLock createWifiLock(int lockType, String tag)
  {
	  // TODO Auto-generated method stub
	  return null;
  }

	@Override
  public WifiLock createWifiLock(String tag)
  {
	  // TODO Auto-generated method stub
	  return null;
  }

	@Override
  public boolean disableNetwork(int netId)
  {
	  // TODO Auto-generated method stub
	  return false;
  }

	@Override
  public boolean disconnect()
  {
	  // TODO Auto-generated method stub
	  return false;
  }

	@Override
  public boolean enableNetwork(int netId, boolean disableOthers)
  {
	  // TODO Auto-generated method stub
	  return false;
  }

	@Override
  public List<WifiConfiguration> getConfiguredNetworks()
  {
	  // TODO Auto-generated method stub
	  return null;
  }

	@Override
  public MyWifiInfo getConnectionInfo()
  {
	counter++;
	Logger.getInstance(ctx).log("WifiSimulator",
			"counter : " + ((Integer) counter).toString());
	if ((counter %2) == 0) {
		if ((counter %2) == 0){
			return new MyWifiInfo("BSSID_"+Integer.toString(counter), false, 1, 1, 1, 1, "SSID_"+Integer.toString(counter), null);
		}
		else
			return new MyWifiInfo("BSSID_"+Integer.toString(counter-1), false, 1, 1, 1, 1, "SSID_"+Integer.toString(counter-1), null);
		}
	else
		return new MyWifiInfo(null, false, -1, -1, -1, -1, null, null);
  }

	@Override
  public DhcpInfo getDhcpInfo()
  {
	  // TODO Auto-generated method stub
	  return null;
  }

	@Override
  public List<MyScanResult> getScanResults()
  {
    return scanResults;
  }

	@Override
  public int getWifiState()
  {
    return enabled ? WifiManager.WIFI_STATE_ENABLED :
      WifiManager.WIFI_STATE_DISABLED;
  }

	@Override
  public boolean isWifiEnabled()
  {
    return enabled;
  }

	@Override
  public boolean pingSupplicant()
  {
	  // TODO Auto-generated method stub
	  return false;
  }

	@Override
  public boolean reassociate()
  {
	  // TODO Auto-generated method stub
	  return false;
  }

	@Override
  public boolean reconnect()
  {
	  // TODO Auto-generated method stub
	  return false;
  }

	@Override
  public boolean removeNetwork(int netId)
  {
	  // TODO Auto-generated method stub
	  return false;
  }

	@Override
  public boolean saveConfiguration()
  {
	  // TODO Auto-generated method stub
	  return false;
  }

	@Override
  public boolean setWifiEnabled(boolean enabled)
  {
    this.enabled = enabled;
    return true;
  }

	@Override
  public boolean startScan()
  {
    Location loc = null;

    scanResults = new LinkedList<MyScanResult>();
    for (MyAccessPoint ap : wifiSimDB.getAvailableAPs(loc))
      scanResults.add(new MyScanResult(ap));
    return true;
  }

	@Override
  public int updateNetwork(WifiConfiguration config)
  {
	  // TODO Auto-generated method stub
	  return 0;
  }
}